/*
 * jQuery UI Input @VERSION
 *
 * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Input
 *
 * Depends:
 *	ui.core.js
 */
(function($) {

$.widget("ui.input", {

	_init: function() {

		var self = this;

		this.element
			.addClass('ui-helper-reset'
					+ ' ui-corner-all')

			.hover(
				function(){
					if (self.element.is(':enabled')) {
						self.inner.addClass("ui-state-active").addClass("ui-form-hover");
					}
				},
				function(){
					self.inner.removeClass("ui-state-active").removeClass("ui-form-hover");
				}
			)
			.focus(function() {
				if (self.element.is(':enabled')) {
					self.inner.addClass("ui-state-highlight").addClass("ui-form-active");
				}
			})
			.blur(function() {
				self.inner.removeClass("ui-state-highlight").removeClass("ui-form-active");
			})
					
			.attr('aria-describedBy', this.labelids()[0]);
			
			
		if (this.options.validation === true && $.ui.formvalidation) {	
									
			this.outer = $('<span />')
				.addClass(this.element[0].className)
				.addClass('ui-input'
						+ ' ui-widget')
				.insertBefore(this.element);

			this.inner = $('<span />')
				.addClass('ui-input-content'
						+ ' ui-widget-content'
						+ ' ui-form-default'
						+ ' ui-corner-all')
				.appendTo(this.outer);

			this.element			
				.bind('validate', function(event, ui) {
					self._indicate();
				})
				.appendTo(this.inner);

			this.validateIcon = $('<span />')
				.addClass('ui-icon'
						+ ' ui-icon-alert')
				.appendTo(this.inner)
				.hide();

				this.hint = $('<label />')
					.addClass('ui-widget'
							+ ' ui-input-help'
							+ ' ui-widget-content'
							+ ' ui-corner-all')
					.attr('role', 'alert')
					.attr('aria-labelfor', this.element.attr('id'))
					.attr('for', this.element.attr('id'))
					.appendTo(this.inner)
					.hide();

			this.element.formvalidation({
				hint: this.hint,
				hintSpeed: this.options.hintSpeed,
				hintAnimation: this.options.hintAnimation,
				
				rules: this.options.rules
			});
			
		}
	},

	destroy: function() {
		this.element
			.removeClass(
				"ui-helper-reset"
					+ " ui-corner-all")
			.removeAttr("role")
			.removeData("input")
			.unbind(".input")
			.insertBefore(this.outer);

		if (this.validateIcon) {
			this.validateIcon.remove();
		}
		if (this.hint) {
			this.hint.remove();
		}

		this.inner.remove();
		this.outer.remove();

		$.widget.prototype.destroy.apply(this, arguments);

		return this;
	},

	labelids: function() {
		var labelids = [];

		this.element.parents('label')
			.add('label[for='+ this.element.attr('id') +']')
			.not('.ui-input-help')
			.each(function() {
				$(this).contents().each(function() {
					if (this.nodeType === 3 && this.id) {
						labelids.push(this.id);
					}
				});
			});

		return labelids;

	},

	_indicate: function() {
		if (this.element.attr('aria-invalid') === 'true') {
			this.element.removeClass('ui-state-error');
			this.inner.addClass('ui-state-error');
			this.element.removeClass('ui-input-no-icon');

			if (this.validateIcon) {
				this.validateIcon.removeClass('ui-icon-circle-check').addClass('ui-icon-alert');

				if (this.validateIcon.is(':hidden'))
					this.validateIcon[this.options.hintAnimation +"In"](this.options.hintSpeed);
			}
		} else {
			this.inner.removeClass('ui-state-error');
			this.inner.addClass('ui-input-no-icon');

			if (this.validateIcon) {
				this.validateIcon.removeClass('ui-icon-alert').addClass('ui-icon-circle-check');

				if (this.element.attr('aria-required') !== 'true' && this.validateIcon.is(':visible'))
					this.validateIcon[this.options.hintAnimation +"Out"](this.options.hintSpeed);
			}
		}

		return this;
	}
});

$.extend($.ui.input, {
	version: "@VERSION",
	defaults: {
		hint: null,							// selector or selector function
		hintSpeed: 'slow',			// hint display speed
		hintAnimation: 'fade',	// hint display speed

		rules: {}								// formvalidation rules
	}
});

})(jQuery);
